<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAddRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="list"
            >
              <el-table-column
                align="center"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                align="center"
                prop="name"
                label="名称"
                width="240"
              />
              <el-table-column
                align="center"
                prop="description"
                label="描述"
              />
              <el-table-column
                align="center"
                label="操作"
              >
                <template slot-scope="{row}">
                  <el-button
                    size="small"
                    type="success"
                    @click="hShowAS(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEditRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDelEmpy(row.id)"
                  >删除 </el-button>
                </template>
              </el-table-column>

            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="hSwitchPage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              v-model="fromData"
              label-width="120px"
              style="margin-top:50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="fromData.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="fromData.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="fromData.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="fromData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层位置 -->

      <el-dialog
        :title="title"
        :visible="isShowDialog"
        @close="btnCancel"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item
            label="角色名称"
            prop="name"
          >
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item
            label="角色描述"
            prop="description"
          >
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row
          slot="footer"
          type="flex"
          justify="center"
        >
          <el-col :span="6">
            <el-button
              size="small"
              @click="btnCancel"
            >取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹层 -->
      <assign-permission
        ref="assignPermission"
        :show-perm-dialog.sync="showPermDialog"
        :user-id="userId"
      />
    </div>
  </div>
</template>

<script>
import { getCompany, getEmployeesList, delRole, getRoleDetail, editRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
import AssignPermission from './components/assignPermission'
export default {
  name: 'Settings',
  components: {
    AssignPermission
  },
  data () {
    return {
      userId: '',
      showPermDialog: false,
      isShowDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '数据不能为空', trigger: 'blur' },
        { min: 1, max: 12, message: '内容长度在1-12个字符之间', trigger: 'blur' }],
        description: [{ required: true, message: '数据内容不能为空', trigger: 'blur' },
        { min: 1, max: 30, message: '内容长度在1-30个字符之间', trigger: 'blur' }]
      },
      list: [], // 员工列表
      page: { // 页码要求
        page: 1,
        pagesize: 2,
        total: 0
      },
      fromData: { // 公司信息
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    title () {
      return this.roleForm.id ? '编辑部门' : '添加部门'
    }
  },
  created () {
    this.getCompany()
    this.getEmployeesList()
  },
  methods: {
    // 获取公司信息
    async getCompany () {
      this.fromData = await getCompany(this.companyId)
      // console.log(this.fromData)
    },
    // 获取员工列表
    async getEmployeesList () {
      const res = await getEmployeesList(this.page)
      // console.log(res)
      this.list = res.rows
      this.page.total = res.total
    },
    // 根据当前分页页码获取员工列表
    hSwitchPage (newpage) {
      // console.log(newpage)
      this.page.page = newpage
      this.getEmployeesList()
    },
    // 删除员工列表
    async hDelEmpy (id) {
      try {
        // 1、提示是否要删除
        // 2、发送删除请求
        // 3、提示
        // 4、删除数据
        // 5、如果当前数据为1，不能等于0，则页码自动减1
        await this.$confirm('是否要删除此角色?')
        await delRole(id)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.getEmployeesList()
      } catch (error) {
        console.log(error)
      }
    },
    async hEditRole (id) {
      this.roleForm = await getRoleDetail(id)
      // console.log(res)
      this.isShowDialog = true
    },
    hAddRole () {
      this.isShowDialog = true
    },
    async btnOK () {
      try {
        // 点击确定编辑
        // 1、等待表单校验通过结果
        // 2、发送修改请求
        // 3、等待请求结果触发提示
        // 4、关闭弹层
        await this.$refs.roleForm.validate()
        // console.log(this.roleForm)
        if (this.roleForm.id) {
          await editRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.$message.success('操作成功')
        this.getEmployeesList()
        this.isShowDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel () {
      // 3、在编辑关闭后切换到添加时会触发校验，需要清空编辑保存的数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 1、点击取消时重置表单数据
      // 2、关闭弹层
      this.$refs.roleForm.resetFields()
      this.isShowDialog = false
    },
    hShowAS (id) {
      console.log(id)
      this.userId = id
      this.$refs.assignPermission.getRoleDetail(id)
      this.showPermDialog = true
    }
  }
}
</script>

<style>
</style>
